// pages/mine/mineimage/mineimage.js
var api = require('../../../api/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Domin: api.SCENE_HOST,
    userInfo: {},
    avatar: "",
    sex: [{
        name: '0',
        value: '男'
      },
      {
        name: '1',
        value: '女',
        checked: 'true'
      },
    ],
    gender: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    var userInfo = wx.getStorageSync("userInfo");
    var sex = userInfo.sex;
    if (sex == 0) {
      that.setData({
        sex: [{
            name: '0',
            value: '男',
            checked: 'true'
          },
          {
            name: '1',
            value: '女',
          },
        ],
        gender: '0'
      })
    } else {
      that.setData({
        sex: [{
            name: '0',
            value: '男'
          },
          {
            name: '1',
            value: '女',
            checked: 'true'
          },
        ],
        gender: '1'
      })
    }
    that.setData({
      userInfo: userInfo,
      avatar: that.data.Domin + userInfo.avatar
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  selectImgTap: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        console.log("tempFilePaths", tempFilePaths);
        that.setData({
          avatar: tempFilePaths
        })
        wx.uploadFile({
          url: api.UploadAvatar, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: "avatarimg",
          formData: {
          },
          header: {
            'Content-Type': 'multipart/form-data',
            'X-bocang-Authorization': wx.getStorageSync('token')
          },
          success(res) {
            const data = res.data
            wx.showToast({
              title: '成功',
              image: "/images/icon/error_icon.png"
            });
          },
          fail(res) {
            const data = res.data
            wx.showToast({
              title: '失败',
              image: "/images/icon/error_icon.png"
            });
          }
        })
      }
    })
  },

  radioSexChange: function(e) {
    let that = this;
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var value = e.detail.value;
    if (value == null || value == "1") {
      that.setData({
        gender: "1"
      });
    } else if (value == "0") {
      that.setData({
        gender: "0"
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})